#region Usings

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using log4net;
using Newtonsoft.Json;

#endregion

namespace Karma.Framework.Core.Serialization.Util
{
    public class SerializationUtils
    {
        private static ILog LOG = LogManager.GetLogger(typeof (SerializationUtils).Name);

        public static JsonConverter[] GetConverters<T>(MemberInfo type) where T : ConvertersAttribute
        {
            var converters = new List<JsonConverter>();
            if (LOG.IsDebugEnabled)
            {
                LOG.Debug(string.Format("Getting converters for: {0}.", type.Name));
            }
            var atts = type.GetCustomAttributes(true);
            foreach (var att in atts)
            {
                if (att.GetType().Equals(typeof (T)))
                {
                    var convatt = (ConvertersAttribute) att;
                    converters.AddRange(from c in convatt.Types
                                        select (JsonConverter) Activator.CreateInstance(c));
                    break;
                }
            }
            if (LOG.IsDebugEnabled)
            {
                foreach (var converter in converters)
                {
                    LOG.Debug(string.Format("Converter: {0}", converter.GetType().Name));
                }
            }
            return converters.ToArray();
        }
    }
}